package com.kws.tool.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.kws.common.core.utils.StringUtils;
import com.kws.tool.service.MsmService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.security.PrivateKey;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    //RAM 访问控制
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.secret}")
    private String secret;

    //短信签名
    @Value("${aliyun.SignName}")
    private String SignName;
    /*//短信模板
    @Value("${aliyun.TemplateCode}")
    private String TemplateCode;*/
    //找回密码模板
    @Value("${aliyun.TemplateCode.passCode.templateId}")
    private String passCode;
    //办理业务受理拒绝通知模板
    @Value("${aliyun.TemplateCode.acceptFail.templateId}")
    private String acceptFail;
    //办理业务受理成功通知模板
    @Value("${aliyun.TemplateCode.acceptSucess.templateId}")
    private String acceptSucess;
    //办理业务申请通知业务人员模板
    @Value("${aliyun.TemplateCode.applyTopic.templateId}")
    private String applyTopic;
    //停电前短信（beforeOutage）模板
    @Value("${aliyun.TemplateCode.beforeOutage.templateId}")
    private String beforeOutage;
    //已超期未消缺提醒模板
    @Value("${aliyun.TemplateCode.eliminatetopic.templateId}")
    private String eliminatetopic;
    //已过期未消缺提醒模板
    @Value("${aliyun.TemplateCode.uneliminatetopic.templateId}")
    private String uneliminatetopic;
    //用户ivr查询电费-未缴费模板
    @Value("${aliyun.TemplateCode.electricunpaid.templateId}")
    private String electricunpaid;
    //用户ivr查询电费-已缴费但未缴清模板
    @Value("${aliyun.TemplateCode.electricpaid.templateId}")
    private String electricpaid;
    //用户ivr查询电费-已缴费但未缴清模板
    @Value("${aliyun.TemplateCode.paid.templateId}")
    private String paid;
    //停电通知（outageNotice）模板
    @Value("${aliyun.TemplateCode.outageNotice.templateId}")
    private String outageNotice;
    //可用余额提醒短信（accountBalanceRemind）模板
    @Value("${aliyun.TemplateCode.accountBalanceRemind.templateId}")
    private String accountBalanceRemind;
    //超阶梯二档提醒短信（monthLevel3）模板
    @Value("${aliyun.TemplateCode.monthLevel3.templateId}")
    private String monthLevel3;
    //超阶梯一档提醒短信（monthLevel2）模板
    @Value("${aliyun.TemplateCode.monthLevel2.templateId}")
    private String monthLevel2;
    //按电量提醒短信（dateAmountRemind）模板
    @Value("${aliyun.TemplateCode.dateAmountRemind.templateId}")
    private String dateAmountRemind;
    //复电成功短信（afterRecover）模板
    @Value("${aliyun.TemplateCode.afterRecover.templateId}")
    private String afterRecover;
    //复电通知短信（beforeRecover）模板
    @Value("${aliyun.TemplateCode.beforeRecover.templateId}")
    private String beforeRecover;
    //停电成功短信（afterOutage）模板
    @Value("${aliyun.TemplateCode.afterOutage.templateId}")
    private String afterOutage;
    //预警短信（earlyWarn）模板
    @Value("${aliyun.TemplateCode.earlyWarn.templateId}")
    private String earlyWarn;
    //验证码短信模板
    @Value("${aliyun.TemplateCode.code.templateId}")
    private String authCode;










    //发送短信的方法
    @Override
    public boolean sendCode(Map<String, Object> param) {
        String codeType = param.get("codeType").toString();
        if (StringUtils.isEmpty(codeType)){
            throw new RuntimeException("短信模板不能为空！！");
        }
        String smsCode="";
        switch (codeType){
            case "passCode":
                smsCode=passCode;
                break;
            case "acceptFail":
                smsCode=acceptFail;
                break;
            case "acceptSucess":
                smsCode=acceptSucess;
                break;
            case "applyTopic":
                smsCode=applyTopic;
                break;
            case "beforeOutage":
                smsCode=beforeOutage;
                break;
            case "eliminatetopic":
                smsCode=eliminatetopic;
                break;
            case "uneliminatetopic":
                smsCode=uneliminatetopic;
                break;
            case "electricunpaid":
                smsCode=electricunpaid;
                break;
            case "electricpaid":
                smsCode=electricpaid;
                break;
            case "paid":
                smsCode=paid;
                break;
            case "outageNotice":
                smsCode=outageNotice;
                break;
            case "accountBalanceRemind":
                smsCode=accountBalanceRemind;
                break;
            case "monthLevel3":
                smsCode=monthLevel3;
                break;
            case "monthLevel2":
                smsCode=monthLevel2;
                break;
            case "dateAmountRemind":
                smsCode=dateAmountRemind;
                break;
            case "afterRecover":
                smsCode=afterRecover;
                break;
            case "beforeRecover":
                smsCode=beforeRecover;
                break;
            case "afterOutage":
                smsCode=afterOutage;
                break;
            case "earlyWarn":
                smsCode=earlyWarn;
                break;
            case "authCode":
                smsCode=authCode;
                break;

        }
        String phone = param.get("phone").toString();
        if(StringUtils.isEmpty(phone)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关固定的参数
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //设置发送相关的参数
        request.putQueryParameter("PhoneNumbers",phone); //手机号
        request.putQueryParameter("SignName",SignName); //申请阿里云 签名名称
        request.putQueryParameter("TemplateCode",smsCode); //申请阿里云 模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据，转换json数据传递

        try {
            //最终发送
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            return success;
        }catch(Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
